package com.lwk.download2;

import android.Manifest;
import android.app.Activity;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;

import androidx.core.app.ActivityCompat;

import com.lwk.download2.utils.FileUtils;

import java.io.File;


public class MainActivity extends Activity {
    public static final int GENERATE_CODE = 666;
    public static final int DECODE = 1;
    public static final int GENERATE = 2;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
        }
    /**
     * 申请权限.
     */
    private void requestPermission(int requestCode, int mode) {
        if (mode == DECODE) {
            decodePermission(requestCode);
        } else if (mode == GENERATE) {
            generatePermission(requestCode);
        }
    }

    /**
     * 申请权限.
     */
    private void decodePermission(int requestCode) {
        ActivityCompat.requestPermissions(
                this,
                new String[]{Manifest.permission.CAMERA, Manifest.permission.READ_EXTERNAL_STORAGE},
                requestCode);
    }

    /**
     * 申请权限.
     */
    private void generatePermission(int requestCode) {
        ActivityCompat.requestPermissions(
                this,
                new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
                requestCode);
    }

    /**
     * 开始下载文件！
     */
    public void generateCodeBtnClick(View v) {
        requestPermission(GENERATE_CODE, GENERATE);
        String newPath = null;
        File[] files;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            files = getExternalFilesDirs(Environment.MEDIA_MOUNTED);
            for(File file:files){
                Log.e("main", String.valueOf(file));
                if (!String.valueOf(file).substring(0,18).equals("/storage/emulated/"))
                    newPath=String.valueOf(file);
                    System.out.println("判断之后："+newPath);
            }
        }
        FileUtils.copyFolder("/sdcard/Storagefactory/QR",newPath+"/QR",MainActivity.this);
    }

}


